package jfilters.filters;

/*
    Copyright Janne Liljeblad 2008.

    This file is part of JFilters.

    JFilters is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    JFilters is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JFilters.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.image.BufferedImage;

public abstract class SimpleLookUpFilter extends SingleInputFilter
{
	protected int[] lookUp = new int[256];

	protected void initlookUp()
	{
		for( int i = 0; i < 256; i++ )
		{
			double val = (double) i / 255.0;
			lookUp[ i ] = (int) (lut_func( val ) * 255.0);
		}
		
	}

	protected abstract double lut_func( double value );

	public BufferedImage filter( BufferedImage img )
	{
		if( mask == null ) return nmFilter( img );
		else return mFilter( img );
	}
	
	//--- no mask
	private BufferedImage nmFilter( BufferedImage img )
	{
		int r, g, b, a;
 		int[] data = getBank( img );

		for( int i = 0; i < data.length; i++ )
		{
			a = ( data[ i ] >> alpha ) & 0xff;
			r = ( data[ i ] >> red ) & 0xff;
			g = ( data[ i ] >> green ) & 0xff;
			b = data[ i ] & 0xff;

			data[ i ] = a << alpha | lookUp[ r ] << red | lookUp[ g ] << green | lookUp[ b ];
		}
		return img;
	}

	//--- mask set
	private BufferedImage mFilter( BufferedImage img )
	{
/*
		int r, g, b, a;
 		int[] data = getBank( img );
		int rowOff;

		int i,j,pix;
		for( i = startY; i < endY; i++ )
		{
			pixOff = i * img.getWidth() + xOff;
			for( j = 0; i < rowWidth; j++ )
			{
				pix = pixOff + j;
				a = ( data[ i ] >> alpha ) & 0xff;
				r = ( data[ i ] >> red ) & 0xff;
				g = ( data[ i ] >> green ) & 0xff;
				b = data[ i ] & 0xff;
	
				data[ i ] = a << alpha | lookUp[ r ] << red | lookUp[ g ] << green | lookUp[ b ];
			}
		}
		return img
*/
		return null;
	}

	protected void printlookUp()
	{
		for( int i = 0; i < 256; i++ )
		{
			System.out.println(  i + ":" + lookUp[ i ] );
		}
	}
	

}//end class